/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.recordsCenter.service.impl;

import me.zhengjie.modules.his.domain.DrugDictionary;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.recordsCenter.repository.DrugDictionaryRepository;
import me.zhengjie.modules.his.recordsCenter.service.DrugDictionaryService;
import me.zhengjie.modules.his.recordsCenter.service.dto.DrugDictionaryDto;
import me.zhengjie.modules.his.recordsCenter.service.dto.DrugDictionaryQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.service.mapstruct.DrugDictionaryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class DrugDictionaryServiceImpl implements DrugDictionaryService {

    private final DrugDictionaryRepository drugDictionaryRepository;
    private final DrugDictionaryMapper drugDictionaryMapper;

    @Override
    public Map<String,Object> queryAll(DrugDictionaryQueryCriteria criteria, Pageable pageable){
        Page<DrugDictionary> page = drugDictionaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(drugDictionaryMapper::toDto));
    }

    @Override
    public List<DrugDictionaryDto> queryAll(DrugDictionaryQueryCriteria criteria){
        return drugDictionaryMapper.toDto(drugDictionaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DrugDictionaryDto findById(Integer drugId) {
        DrugDictionary drugDictionary = drugDictionaryRepository.findById(drugId).orElseGet(DrugDictionary::new);
        ValidationUtil.isNull(drugDictionary.getDrugId(),"DrugDictionary","drugId",drugId);
        return drugDictionaryMapper.toDto(drugDictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DrugDictionaryDto create(DrugDictionary resources) {
        return drugDictionaryMapper.toDto(drugDictionaryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DrugDictionary resources) {
        DrugDictionary drugDictionary = drugDictionaryRepository.findById(resources.getDrugId()).orElseGet(DrugDictionary::new);
        ValidationUtil.isNull( drugDictionary.getDrugId(),"DrugDictionary","id",resources.getDrugId());
        drugDictionary.copy(resources);
        drugDictionaryRepository.save(drugDictionary);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer drugId : ids) {
            drugDictionaryRepository.deleteById(drugId);
        }
    }

    @Override
    public void download(List<DrugDictionaryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugDictionaryDto drugDictionary : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药品名称", drugDictionary.getDrugName());
            map.put("计量单位", drugDictionary.getUnitId());
            map.put("售价", drugDictionary.getSellingPrice());
            map.put("产地", drugDictionary.getAreaId());
            map.put("类型", drugDictionary.getTypeId());
            map.put("创建者", drugDictionary.getCreateBy());
            map.put("更新者", drugDictionary.getUpdateBy());
            map.put("创建日期", drugDictionary.getCreateTime());
            map.put("更新时间", drugDictionary.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}